package avicit.im.lantop.basic.reportmanager.service.api;


import avicit.im.lantop.basic.reportmanager.domain.LantopReportConfigDetails;
import avicit.im.lantop.basic.reportmanager.domain.LantopReportConfiguration;
import avicit.im.lantop.basic.reportmanager.domain.LantopReportTemplate;
import avicit.platform6.commons.utils.web.TreeNode;
import avicit.platform6.core.dao.Paging;
import avicit.platform6.core.rest.msg.ResponseMsg;
import avicit.platform6.core.restclient.RestClient;
import avicit.platform6.core.restclient.RestClientUtils;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ReportManagerServicesApi {



    /**
     * 服务编码
     */
    private static final String SERVICE_CODE = "filebasicmanager";

    private static final String BASE_PATH = "/api/avicit/im/lantop/basic/reportmanager/rest/ReportManagerServicesRest";

    @Autowired
    private RestClient restClient;


    /**
     *查找模板实际输出字段列表
     * @param argsMaps
     * @return
     */
    public List<Map<String, Object>> findReportSetField(Map<String, String> argsMaps) {
        String url = BASE_PATH + "/findReportSetField";
        Map<String,Object> map = new HashMap<>();
        map.put("argsMaps",argsMaps);
        ResponseMsg<List<Map<String, Object>>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<List<Map<String, Object>>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 查询DataSource数据表
     * @param argsMaps
     * @return
     */
    public List<Map<String, Object>> findDataSource(Map<String, String> argsMaps) {
        String url = BASE_PATH + "/findDataSource";
        Map<String,Object> map = new HashMap<>();
        map.put("argsMaps",argsMaps);
        ResponseMsg<List<Map<String, Object>>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<List<Map<String, Object>>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 模板id查询报表配置
     * @param reportId
     * @return
     */
    public List<Map<String, Object>> findLantopReportConfigByTemplateId(String reportId) {
        String url = BASE_PATH + "/findLantopReportConfigByTemplateId";
        Map<String,Object> map = new HashMap<>();
        map.put("reportId",reportId);
        ResponseMsg<List<Map<String, Object>>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<List<Map<String, Object>>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 根据报表ID查找Name
     * @param reportId
     * @return
     */
    public String getReportRealNameById(String reportId) {
        String url = BASE_PATH + "/getReportRealNameById";
        Map<String,Object> map = new HashMap<>();
        map.put("reportId",reportId);
        ResponseMsg<String> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<String>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 根据表ID和数据节点ID获取二维码规则信息
     * @param tableNameIds
     * @param nodeId
     * @return
     */
    public List<Map<String, Object>> findRuleInfoByTableId(String tableNameIds, String nodeId) {
        String url = BASE_PATH + "/findRuleInfoByTableId";
        Map<String,Object> map = new HashMap<>();
        map.put("tableNameIds",tableNameIds);
        map.put("nodeId",nodeId);
        ResponseMsg<List<Map<String, Object>>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<List<Map<String, Object>>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     *根据表ID获取二维码规则信息
     * @param tableNameIds
     * @return
     */
    public List<Map<String, Object>> findRuleInfoByTableId(String tableNameIds) {
        String url = BASE_PATH + "/findRuleInfoByTableId/v1";
        Map<String,Object> map = new HashMap<>();
        map.put("tableNameIds",tableNameIds);
        ResponseMsg<List<Map<String, Object>>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<List<Map<String, Object>>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 保存报表临时数据表信息
     * @param temp
     * @return
     */
    public String saveReportTemplate(LantopReportTemplate temp) {
        String url = BASE_PATH + "/saveReportTemplate";
        Map<String,Object> map = new HashMap<>();
        map.put("temp",temp);
        ResponseMsg<String> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<String>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 删除树节点
     * @param paramsMap
     * @return
     */
    public boolean deleteLantopReportTree(Map<String, Object> paramsMap) {
        String url = BASE_PATH + "/deleteLantopReportTree";
        Map<String,Object> map = new HashMap<>();
        map.put("paramsMap",paramsMap);
        ResponseMsg<Boolean> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<Boolean>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 递归查询树结构
     * @param id
     * @param orgId
     * @param orgIdentity
     * @param level
     * @param contextPath
     * @return
     */
    public List<TreeNode> getTreeExerciseByParentId(String id, String orgId, String orgIdentity, int level, String contextPath) {
        String url = BASE_PATH + "/getTreeExerciseByParentId";
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("orgId",orgId);
        map.put("orgIdentity",orgIdentity);
        map.put("level",level);
        map.put("contextPath",contextPath);
        ResponseMsg<List<TreeNode>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<List<TreeNode>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 插入模板树数据
     * @param param
     */
    public Boolean insertIntoLantopReportTree(Map<String, Object> param) {
        String url = BASE_PATH + "/insertIntoLantopReportTree";
        Map<String,Object> map = new HashMap<>();
        map.put("param",param);
        ResponseMsg<Boolean> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<Boolean>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 通过配置表名，查找已配置过的模板树
     * @param paramsMap
     * @return
     */
    public List<TreeNode> findTreeByTableName(Map<String, Object> paramsMap) {
        String url = BASE_PATH + "/findTreeByTableName";
        Map<String,Object> map = new HashMap<>();
        map.put("paramsMap",paramsMap);
        ResponseMsg<List<TreeNode>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<List<TreeNode>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 删除模块对应关系
     * @param reportId
     * @param tableName
     * @param orgId
     */
    public void deleteReportRelation(String reportId, String tableName, String orgId) {
        String url = BASE_PATH + "/deleteReportRelation";
        Map<String,Object> map = new HashMap<>();
        map.put("reportId",reportId);
        map.put("tableName",tableName);
        map.put("orgId",orgId);
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg>() {
        });
    }

    /**
     *
     * @param tempId
     * @return
     */
    public LantopReportTemplate getLantopReportTemplateById(String tempId) {
        String url = BASE_PATH + "/getLantopReportTemplateById";
        Map<String,Object> map = new HashMap<>();
        map.put("tempId",tempId);
        ResponseMsg<LantopReportTemplate> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<LantopReportTemplate>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 保存DataBase数据
     * @param paramsMap
     * @return
     */
    public boolean saveDataBaseConfigForTable(Map<String, Object> paramsMap) {
        String url = BASE_PATH + "/saveDataBaseConfigForTable";
        Map<String,Object> map = new HashMap<>();
        map.put("paramsMap",paramsMap);
        ResponseMsg<Boolean> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<Boolean>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 查找来源于数据库模板应输出字段
     * @param reportId
     * @param paramsMap
     * @return
     */
    public List<String> findDataSourceFields(String reportId, Map<String, Object> paramsMap) {
        String url = BASE_PATH + "/findDataSourceFields";
        Map<String,Object> map = new HashMap<>();
        map.put("reportId",reportId);
        map.put("paramsMap",paramsMap);
        ResponseMsg<List<String>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<List<String>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 插入模板树数据
     * @param params
     */
    public void insertIntoLantopReportTreeForUpload(Map<String, String> params) {
        String url = BASE_PATH + "/insertIntoLantopReportTreeForUpload";
        Map<String,Object> map = new HashMap<>();
        map.put("params",params);
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg>() {
        });
    }


    /**
     * 插入模板树数据
     * @param nodeId
     * @param orgId
     * @return
     */
    public List<Map<String, Object>> findTablesByNodeId(String nodeId, String orgId) {
        String url = BASE_PATH + "/findTablesByNodeId";
        Map<String,Object> map = new HashMap<>();
        map.put("nodeId",nodeId);
        map.put("orgId",orgId);
        ResponseMsg<List<Map<String, Object>>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<List<Map<String, Object>>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 通过报表模板id查询报表配置规则主表
     * @param templateId
     * @return
     */
    public String findLantopReportConfigurationByTemplateId(String templateId) {
        String url = BASE_PATH + "/findLantopReportConfigurationByTemplateId";
        Map<String,Object> map = new HashMap<>();
        map.put("templateId",templateId);
        ResponseMsg<String> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<String>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     *
     * @param dto
     */
    public void saveLantopReportConfiguration(LantopReportConfiguration dto) {
        String url = BASE_PATH + "/saveLantopReportConfiguration";
        Map<String,Object> map = new HashMap<>();
        map.put("dto",dto);
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<String>>() {
        });
    }

    /**
     * 报表模板配置规则保存子表
     * @param lantopReportConfigDetails
     */
    public void saveReportConfig(LantopReportConfigDetails lantopReportConfigDetails) {
        String url = BASE_PATH + "/saveReportConfig";
        Map<String,Object> map = new HashMap<>();
        map.put("lantopReportConfigDetails",lantopReportConfigDetails);
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<String>>() {
        });
    }


    /**
     * 报表模板配置规则更新子表
     * @param lantopReportConfigDetails
     */
    public void updateReportConfig(LantopReportConfigDetails lantopReportConfigDetails) {
        String url = BASE_PATH + "/updateReportConfig";
        Map<String,Object> map = new HashMap<>();
        map.put("lantopReportConfigDetails",lantopReportConfigDetails);
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<String>>() {
        });
    }


    /**
     * 通过子表id查询主表
     * @param s
     * @return
     */
    public String findLantopReportConfigurationIdById(String s) {
        String url = BASE_PATH + "/findLantopReportConfigurationIdById";
        Map<String,Object> map = new HashMap<>();
        map.put("s",s);
        ResponseMsg<String> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<String>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 报表模板配置规则子表根据id删除数据
     * @param ids
     */
    public void deleteReportConfigByIds(String ids) {
        String url = BASE_PATH + "/deleteReportConfigByIds";
        Map<String,Object> map = new HashMap<>();
        map.put("ids",ids);
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<String>>() {
        });
    }

    /**
     *
     * @param reportConfigPid
     * @return
     */
    public int findLantopReportConfigDetailsByPid(String reportConfigPid) {
        String url = BASE_PATH + "/findLantopReportConfigDetailsByPid";
        Map<String,Object> map = new HashMap<>();
        map.put("reportConfigPid",reportConfigPid);
        ResponseMsg<Integer> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<Integer>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 根据id删除报表模板配置规则主表
     * @param reportConfigPid
     */
    public void deleteLantopReportConfigurationById(String reportConfigPid) {
        String url = BASE_PATH + "/deleteLantopReportConfigurationById";
        Map<String,Object> map = new HashMap<>();
        map.put("reportConfigPid",reportConfigPid);
        ResponseMsg responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg>() {
        });
    }


    /**
     * 报表模板配置规则子表查询
     * @param page
     * @param tMap
     * @return
     */
    public Paging<LantopReportConfigDetails> getReportConfig(Paging<LantopReportConfigDetails> page, Map<String, Object> tMap) {
        String url = BASE_PATH + "/getReportConfig";
        Map<String,Object> map = new HashMap<>();
        map.put("page",page);
        map.put("tMap",tMap);
        ResponseMsg<Paging<LantopReportConfigDetails>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<Paging<LantopReportConfigDetails>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     *
     * @param id
     * @param level
     * @param orgId
     * @return
     */
    public List<TreeNode> recurFindTreeNodesByParentId(String id, int level, String orgId) {
        String url = BASE_PATH + "/recurFindTreeNodesByParentId";
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("level",level);
        map.put("orgId",orgId);
        ResponseMsg<List<TreeNode>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<List<TreeNode>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     *
     * @return
     */
    public List<Map<String, Object>> getPublicFieldInfo() {
        String url = BASE_PATH + "/getPublicFieldInfo";
        Map<String,Object> map = new HashMap<>();
        ResponseMsg<List<Map<String, Object>>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<List<Map<String, Object>>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 根据模板ID删除其对应的字段配置信息
     * @param paramsMap
     * @return
     */
    public boolean deleteFieldInfosById(Map<String, Object> paramsMap) {
        String url = BASE_PATH + "/deleteFieldInfosById";
        Map<String,Object> map = new HashMap<>();
        map.put("paramsMap",paramsMap);
        ResponseMsg<Boolean> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<Boolean>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     * 保存用户自定义数据源
     * @param paramsMap
     * @return
     */
    public boolean saveUseCustomDataSource(Map<String, Object> paramsMap) {
        String url = BASE_PATH + "/saveUseCustomDataSource";
        Map<String,Object> map = new HashMap<>();
        map.put("paramsMap",paramsMap);
        ResponseMsg<Boolean> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<Boolean>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }

    /**
     * 查找用户自定义数据源应输出字段
     * @param reportId
     * @param paramsMap
     * @return
     */
    public List<String> findUseCustomDataSourceFields(String reportId, Map<String, Object> paramsMap) {
        String url = BASE_PATH + "/findUseCustomDataSourceFields";
        Map<String,Object> map = new HashMap<>();
        map.put("reportId",reportId);
        map.put("paramsMap",paramsMap);
        ResponseMsg<List<String>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<List<String>>>() {
        });
        return RestClientUtils.getResponseBody(responseMsg);
    }


    /**
     * 根据模板ID查找其对应的字段配置信息
     * @param paramsMap
     * @param page
     */
    public void findFieldInfosByReportid(Map<String, Object> paramsMap, Page<Map<String, Object>> page) {
        String url = BASE_PATH + "/findFieldInfosByReportid";
        Map<String,Object> map = new HashMap<>();
        map.put("paramsMap",paramsMap);
        map.put("page",page);
        ResponseMsg<List<String>> responseMsg = restClient.doPost(SERVICE_CODE, url, map, new ParameterizedTypeReference<ResponseMsg<List<String>>>() {
        });
    }
}
